package com.bwie.shop.controller;

import com.alibaba.fastjson.JSONObject;
import com.bwie.common.domain.Card;
import com.bwie.common.domain.Shop;
import com.bwie.common.domain.request.CouponRequest;
import com.bwie.common.domain.request.ShopRequest;
import com.bwie.common.result.PageResult;
import com.bwie.common.result.Result;
import com.bwie.shop.service.ShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private ShopService shopService;

    @Autowired
    private HttpServletRequest request;


    @Transactional
    @PostMapping("/showShop")
    public Result<PageResult<Shop>> showShop(@RequestBody ShopRequest shopRequest){
        log.info("功能名称:商品列表,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(shopRequest));
        Result<PageResult<Shop>> result = shopService.showShop(shopRequest);
        log.info("功能名称:商品列表,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(result));
        return result;
    }


    @PostMapping("/findById")
    public Result findById(@RequestParam String shopId){
        log.info("功能名称:购买回显,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                shopId);
        Result byId = shopService.findById(shopId);
        log.info("功能名称:购买回显,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(byId));
        return byId;
    }

    @PostMapping("/showCoupon")
    public Result showCoupon(){
        log.info("功能名称:优惠卷列表,请求URI:{},请求方式:{}",
                request.getRequestURI(),
                request.getMethod()
               );
        Result result = shopService.showCoupon();
        log.info("功能名称:优惠卷列表,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(result));
        return result;
    }


    @PostMapping("/addCoupon")
    public Result addCoupon(@RequestBody CouponRequest couponRequest){
        log.info("功能名称:添加优惠券,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(couponRequest));
        Result result = shopService.addCoupon(couponRequest);
        log.info("功能名称:添加优惠券,请求URI:{},请求方式:{},响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(result));
        return result;
    }

    @PostMapping("/deleteCoupon")
    public Result deleteCoupon(@RequestParam String couponId){
        log.info("功能名称:删除优惠券,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                couponId);
        Result result = shopService.deleteCoupon(couponId);
        log.info("功能名称:删除优惠券,请求URI:{},请求方式:{},响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(result));
        return result;
    }

    @PostMapping("/showCard")
    public Result showCard(){
        log.info("功能名称:我的优惠卷,请求URI:{},请求方式:{}",
                request.getRequestURI(),
                request.getMethod()
        );
        Result result = shopService.showCard();
        log.info("功能名称:我的优惠卷,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(result));
        return result;
    }

    @PostMapping("/updateStatus")
    public Result updateStatus(@RequestBody Card card){
        log.info("功能名称:商品下单,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(card));
        Result result = shopService.updateStatus(card);
        log.info("功能名称:商品下单,请求URI:{},请求方式:{},响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(result));
        return result;
    }

    @PostMapping("/bringCoupon")
    public Result bringCoupon(@RequestBody CouponRequest couponRequest){
        log.info("功能名称:领取,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(couponRequest));
        Result result = shopService.bringCoupon(couponRequest);
        log.info("功能名称:领取,请求URI:{},请求方式:{},响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(result));
        return result;
    }



}
